package B1;

import com.github.oxo42.stateless4j.StateMachine;
import com.github.oxo42.stateless4j.StateMachineConfig;

public class Thermostatic_water_tank {

    private StateMachineConfig<State, Event> config;

    private GuardCondition guardCondition = new GuardCondition();
    private StateMachine<State, Event> stateMachine;

    public Thermostatic_water_tank() {
        init();
    }

    public void init() {
        guardCondition.setEmpty(false);
        guardCondition.setTmp_is_low(false);

        // 创建状态机配置
        config = new StateMachineConfig<>();

        // 定义状态转换规则
        config.configure(State.Relay_power_off)
                .permitIf(Event.Relay_power_turnon, State.Relay_power_on, guardCondition.is_low_and_is_not_empty(),new StartThermostatic_water_tankAction())
                .ignoreIf(Event.Relay_power_turnon, guardCondition.is_not_low_or_is_empty())
                .permitIf(Event.toSleep,State.Sleep,guardCondition.isNotTime(),new StartSleepAction())
                .ignore(Event.Relay_power_turnoff);


        config.configure(State.Relay_power_on)
                .permitIf(Event.Relay_power_turnoff, State.Relay_power_off, guardCondition.is_not_low_or_is_empty(),new StopThermostatic_water_tankAction())
                .ignoreIf(Event.Relay_power_turnoff,guardCondition.is_low_and_is_not_empty())
                .permitIf(Event.toSleep,State.Sleep,guardCondition.isNotTime(),new StartSleepAction())
                .ignore(Event.Relay_power_turnon)
                .permit(Event.burningOut,State.Burning_out);

        config.configure(State.Sleep)
                .permitIf(Event.Relay_power_turnoff, State.Relay_power_off,guardCondition.isTime(),new StopSleepAction())
                .permitIf(Event.Relay_power_turnon, State.Relay_power_on,guardCondition.isTime_and_can_fire(),new StartThermostatic_water_tankAction())
                .ignore(Event.toSleep)
                .ignoreIf(Event.Relay_power_turnon,guardCondition.isNotTime())
                .ignoreIf(Event.Relay_power_turnoff,guardCondition.isNotTime());


        config.configure(State.Burning_out)
                .ignore(Event.Relay_power_turnon)
                .ignore(Event.Relay_power_turnoff)
                .ignore(Event.burningOut);

        // 创建状态机实例
        stateMachine = new StateMachine<>(State.Sleep, config);
    }

    public void setEmptyWater(boolean empty) {
        this.guardCondition.setEmpty(empty);
    }
    public void setTep_is_low(boolean tep_is_low){this.guardCondition.setTmp_is_low(tep_is_low);}
    public void fire(Event event) {
        this.stateMachine.fire(event);
    }

    public State getCurrentState() {
        return this.stateMachine.getState();
    }
    public void printCurrentState() {
        System.out.println("Current State: " + stateMachine.getState());
    }
}

